package com.sh4.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.maven.shared.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sh4.dao.impl.XgfsBookDaoImpl;
import com.sh4.dao.impl.XgfsOrderDaoImpl;
import com.sh4.entity.XgfsOrder;
import com.sh4.service.XgfsOrderService;
import com.sh4.util.ContextHeader;

import net.sf.json.JSONObject;

@Service("xgfsOrderServiceImpl")
@Transactional
public class XgfsOrderServiceImpl implements XgfsOrderService{
	
	@Autowired
	XgfsOrderDaoImpl xgfsOrderDaoImpl;
	
	@Autowired
	XgfsBookDaoImpl xgfsBookDaoImpl;

	@Override
	public void save(JSONObject params) {
		
		/***
		 * 1.将预约信息转订单信息 更新预约状态为已转订单
		 */
		if(params.containsKey("id") && StringUtils.isNotEmpty(params.getString("id"))) {//根据id去更新预约状态
			xgfsBookDaoImpl.executeUpdateBySql("update xgfs_book xb set xb.BOOK_STATUS_ = ? , xb.UPDATE_DATE_ = ? , xb.UPDATE_BY_ = ? where xb.ID_ = ?", "xgfs10002",new Date(),ContextHeader.getUserCode(),params.getString("id"));
		}
		
		
		/***
		 * 2.保存要转的定单信息
		 */
		XgfsOrder order = new XgfsOrder();
		
		order.setBookAddress(params.getString("bookAddress"));
		order.setBookArea(params.getDouble("bookArea"));
		order.setBookMobile(params.getString("bookMobile"));
		order.setBookStatus("xgfs11000");//表示全新订单
		order.setBookType(params.getString("bookType"));
		order.setCreateDate(new Date());
		order.setBookId(params.getString("id"));
		order.setOpenId(params.getString("openId"));
		order.setOrderType(params.getString("orderType"));
		order.setRemark(params.getString("remark"));
		
		xgfsOrderDaoImpl.saveOrUpdate(order);
	}

	@Override
	public List<Map<String, Object>> list(int pageSize, int pageNo, JSONObject params) {
		
		return xgfsOrderDaoImpl.list(pageSize, pageNo, params);
	}

	@Override
	public void deleteXgfsOrder(XgfsOrder xb) {
		
		xgfsOrderDaoImpl.delete(xb);
		
	}

	@Override
	public XgfsOrder getXgfsOrderById(String id) {
		
		return xgfsOrderDaoImpl.getXgfsOrderById(id);
	}


	@Override
	public int countXgfsOrder(JSONObject params) {
		
		return xgfsOrderDaoImpl.countXgfsOrder(params);
	}

	@Override
	public void commentXgfsOrder(JSONObject xb) {
		
		xgfsBookDaoImpl.executeUpdateBySql("update xgfs_order xo set xo.ATTIDUTE_ = ?, xo.MAINTENANCE_SPEED_ = ? , xo.MAINTENANCE_QUALITY_ = ? , xo.MAINTENANCE_COMMENT_ = ? ,xo.UPDATE_DATE_ = ?,xo.BOOK_STATUS_ = ? where xo.ID_ = ?", 
				xb.getString("attitude"), xb.getString("speed"),xb.getString("quality"),xb.getString("comment"),new Date(),"xgfs11002",xb.getString("id"));
		
	}

	@Override
	public void uptadeOrderComplete(JSONObject xo, String bookStatus) {
		
		//计算保修时间  
		Date complete = new Date();
		int inter = 0;
		if(xo.containsKey("guanteeValue")) {
			inter = Integer.parseInt(xo.getString("guanteeValue"));
		}
		Calendar c = Calendar.getInstance();
		c.setTime(complete);
		c.add(Calendar.YEAR, inter);
		
		Date guanteeDate = c.getTime();
		
		String hql = "update XgfsOrder set bookStatus = ?, updateDate = now() ,updateBy = ? , completeDate = ? ,guaranteeDate = ? where id = ?";
		
		xgfsBookDaoImpl.executeUpdate(hql, bookStatus ,ContextHeader.getUserCode() , complete , guanteeDate , xo.getString("id"));
	}

	
}
